package com.liu.StatisticService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.R;
import com.liu.StatisticService.client.UcenterClient;
import com.liu.StatisticService.entity.StatisticsDaily;
import com.liu.StatisticService.mapper.StatisticsDailyMapper;
import com.liu.StatisticService.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author LSJ
 * @since 2022-02-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) {

        // 每次添加之前先删除
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        // 获取某天用户注册人数
        R r = ucenterClient.getRegisterMemberCount(day);
        Map<String, Object> data = r.getData();
        int count = (int) data.get("RegisterMemberCount");


        StatisticsDaily statistics = new StatisticsDaily();
        statistics
                // 统计日期：day
                .setDateCalculated(day)
                .setRegisterNum(count)
//                剩下都是捏造的
                .setLoginNum( RandomUtils.nextInt(100, 200))
                .setVideoViewNum( RandomUtils.nextInt(100, 200))
                .setCourseNum( RandomUtils.nextInt(100, 200));
        baseMapper.insert(statistics);

    }

    @Override
    public Map<String, Object> showData(String type, String begin, String end) {

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        // 根据 处于开始结束内的 日期,type：
        wrapper.between("date_calculated",begin,end);
        wrapper.select(type,"date_calculated");
//      select type,date_calculated from statistic where date_calculated between begin and end;
        List<StatisticsDaily> selectList = baseMapper.selectList(wrapper);

        Map<String, Object> map = new HashMap<>();
        List<String> dateCalculated = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (StatisticsDaily daily : selectList) {
            //            日期列表
            dateCalculated.add(daily.getDateCalculated());
//            确定记录哪个 数据
            switch (type) {
                case "register_num":
                    numList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }

        }

        map.put("dateCalculated",dateCalculated);
        map.put("numList",numList);
        return  map;

    }
}
